package com.gmu.controller;


import com.alibaba.excel.EasyExcel;
import com.gmu.domain.ResResult;
import com.gmu.domain.vo.PageVo;
import com.gmu.domain.vo.TblSampleVo;
import com.gmu.mapper.TblSampleMapper;
import com.gmu.pojo.Page;
import com.gmu.pojo.TblActivities;
import com.gmu.pojo.TblSample;
import com.gmu.service.TblSampleService;
import com.gmu.utils.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 采样记录表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-07-24
 */
@Api(value = "采样记录管理",tags = "采样记录管理API")
@RestController
@RequestMapping("/gmu/tbl-sample")
public class TblSampleController {

    @Autowired
    private TblSampleService tblSampleService;

    @Autowired
    private TblSampleMapper tblSampleMapper;

    /**
     * 提交采样记录(第一次扫码)
     * @param unitsId 单位id
     * @param userId 用户id
     * @param idNumber 身份证号
     * @param activitiesId 活动id
     * @return
     */
    @ApiOperation(value = "提交采样记录(第一次扫码)")
    @PostMapping("/commitSampleRecord")
    public ResResult<?> commitSampleRecord(String unitsId, String userId, String idNumber, String activitiesId){
        return tblSampleService.commitSampleRecord(unitsId,userId,idNumber,activitiesId);
    }

    /**
     * 提交检测时间(第二次扫码)
     * @param idNumber 身份证号
     * @param activitiesId 活动id
     * @return
     */
    @ApiOperation(value = "提交检测时间(第二次扫码)")
    @PostMapping("/commitTestTime")
    public ResResult<?> commitTestTime(String idNumber, String activitiesId){
        return tblSampleService.commitTestTime(idNumber,activitiesId);
    }

    /**
     * 获取某个地区的采样记录
     */
    @ApiOperation("获取某个地区的采样记录")
    @GetMapping("/getSampleRecordByArea/{activitiesId}/{unitsId}/{status}")
    public ResResult<?> getSampleRecordByArea(@PathVariable Integer activitiesId,
                                              @PathVariable Integer unitsId,
                                              @PathVariable Integer status){
        return tblSampleService.getSampleRecordByArea(activitiesId,unitsId,status);
    }

    /**
     * 获取某用户的采样记录
     */
    @ApiOperation(value = "获取某用户的采样记录")
    @GetMapping("/getUserSampleByUserId/{userId}")
    public ResResult<?> getUserSampleByUserId(@PathVariable Integer userId){
       return tblSampleService.getUserSampleByUserId(userId);
    }

    @ApiOperation(value = "pc端获取所有采样记录")
    @GetMapping("/sampleRecordList")
    public PageVo<?> getSampleRecordList(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize, TblSampleVo tblSample, String beginTime, String endTime) {
        return tblSampleService.getSampleRecordList(pageNum,pageSize,tblSample,beginTime,endTime);
    }

    @ApiOperation(value = "pc端批量删除采样记录")
    @DeleteMapping("/deleteSampleRecord/{sampleRecordIds}")
    public ResResult<?> deleteSampleRecord(@PathVariable Integer[] sampleRecordIds) {
        return tblSampleService.deleteSampleRecord(sampleRecordIds);
    }

    @ApiOperation(value = "导出采样记录")
    @GetMapping("/exportRecord")
    public void export(HttpServletResponse response, TblSampleVo tblSample, String beginTime, String endTime) {
        try {
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode("核酸采样记录","UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Access-Control-Expose-Headers","Content-disposition");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            List<TblSampleVo> list = tblSampleService.getSampleVoList(tblSample,beginTime,endTime);
            EasyExcel.write(response.getOutputStream(), TblSampleVo.class).sheet("采样记录").doWrite(list);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取所有采样记录(分页)
     */
    @PreAuthorize("hasAuthority('sys:notesmanage')")
    @ApiOperation(value = "获取所有采样记录(分页)")
    @PostMapping("/getAllSampleByPage")
    public ResResult<?> getAllSampleByPage(@RequestBody Page page){
        Map<String, Object> pageMap = PageUtil.getPageMap(page);
        List<TblSample> tblSamples = tblSampleMapper.getAllSampleByPage(pageMap);
        Integer total = tblSampleMapper.selectCount(null);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("tblSamples",tblSamples);
        return new ResResult<>(200,map);
    }

    /**
     * 修改采样记录
     */
    @PreAuthorize("hasAuthority('sys:notesmanage')")
    @ApiOperation(value = "修改采样记录")
    @PutMapping("/updateSampleById")
    public ResResult<?> updateSampleById(@RequestBody TblSample tblSample){
        if ((tblSampleMapper.updateById(tblSample)) < 1){
            return new ResResult<>(500,"修改失败");
        }
        return new ResResult<>(200,"修改成功");
    }

    /**
     * 删除采样记录
     */
    @PreAuthorize("hasAuthority('sys:notesmanage')")
    @ApiOperation(value = "删除采样记录")
    @DeleteMapping("/deleteSampleById/{sampleId}")
    public ResResult<?> deleteSampleById(@PathVariable Integer sampleId){
        if ((tblSampleMapper.deleteById(sampleId)) < 1){
            return new ResResult<>(500,"删除失败");
        }
        return new ResResult<>(200,"删除成功");
    }

    /**
     * PC端获取某次活动的学生信息
     * @param activitiesId
     * @return
     */
    @ApiOperation(value = "PC端获取某次活动的学生信息")
    @PostMapping("/getStudentByActivitiesId")
    public ResResult<?> getStudentByActivitiesId(@RequestBody Integer activitiesId){
        return tblSampleService.getStudentByActivitiesId(activitiesId);
    }

    /**
     * PC端获取某次活动的教师信息
     * @param activitiesId
     * @return
     */
    @ApiOperation(value = "PC端获取某次活动的教师信息")
    @GetMapping("/getTeacherByActivitiesId/{activitiesId}")
    public ResResult<?> getTeacherByActivitiesId(@PathVariable Integer activitiesId){
        return tblSampleService.getTeacherByActivitiesId(activitiesId);
    }



}

